package com.pdwbdn.views;

import javax.swing.text.AttributeSet;
import javax.swing.text.DocumentFilter;

public class Abc123DocumentFilter extends DocumentFilter {
    private boolean acceptsOnlyAbc123(String string) {
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (!Character.isDigit(c) && !Character.isLowerCase(c) && !Character.isUpperCase(c)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) {
        if (string == null) {
            return;
        }
        if (acceptsOnlyAbc123(string)) {
            try {
                super.insertString(fb, offset, string, attr);
            } catch (Throwable t) {
                System.err.println(t.toString());
            }
        }
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) {
        if (text == null) {
            return;
        }
        if (acceptsOnlyAbc123(text)) {
            try {
                super.replace(fb, offset, length, text, attrs);
            } catch (Throwable t) {
                System.err.println(t.toString());
            }
        }
    }
}
